package com.service;

import java.util.List;

import com.model.Menu;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

/**   
 *   
 * @author zhanghusong   
 */
public class MenuServiceImpl extends AbstractDateService implements MenuService {

	@Override
	public boolean deleteMenu(Menu menu) {
		String sql = "delete from t_menu where id="+menu.getId();
		logger.info("[SQL] [method:deleteMenu] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base,sql);
	}

	@Override
	public List<Menu> getMenu() {
		String sql = "select id, menuname menuName, menuurl menuUrl, parentid parentId,  " +
				"menuvalue menuValue, createtime createTime, createuser createUser,ordernum orderNum from t_menu order by ordernum";
		logger.info("[SQL] [method:getMenu] ["+sql+"]");
		ModelGenerator<Menu> model = new ModelGenerator<Menu>(Menu.class);
		return model.generateList(base,sql.toString());
	}

	@Override
	public Menu getMenuById(Menu menu) {
		String sql = "select id, menuname menuName, menuurl menuUrl, parentid parentId,  " +
				"menuvalue menuValue, createtime createTime, createuser createUser,ordernum orderNum from t_menu where id="+menu.getId()+" order by ordernum";
		logger.info("[SQL] [method:getMenuById] ["+sql+"]");
		ModelGenerator<Menu> model = new ModelGenerator<Menu>(Menu.class);
		return model.generate(base,sql.toString());
	}

	@Override
	public boolean saveMenu(Menu menu) {
		String sql = "insert into t_menu (id, menuname, menuurl, parentid, menuvalue," +
				" createtime, createuser, ordernum) " +
				"values ("+getTableId("t_menu")+", '"+menu.getMenuName()+
				"', '"+menu.getMenuUrl()+"', '"+menu.getParentId()+"', '"+menu.getMenuValue()+"', now(), '"+menu.getCreateUser()+"', "+menu.getOrderNum()+")";
		logger.info("[SQL] [method:saveMenu] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base,sql);
	}

	@Override
	public boolean updateMenu(Menu menu) {
		String sql = "update t_menu set menuname = '"+menu.getMenuName()+
		"',menuurl = '"+menu.getMenuUrl()+"',ordernum = "+menu.getOrderNum()+",parentid="+menu.getParentId()+" where id= "+menu.getId();
		logger.info("[SQL] [method:updateMenu] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base,sql);
	}

}
